<template>
  <div class="container">
    <el-card>
      <SearchForm @search="search" @resetList="resetList"></SearchForm>
      <el-alert
        :title="'数据一共' + page.total + '条'"
        type="info"
        show-icon
        :closable="false"
      >
      </el-alert>
      <el-table
        :data="question"
        style="width: 100%"
        :header-cell-style="{ background: '#fafafa' }"
      >
        <el-table-column prop="number" label="试题编号"></el-table-column>
        <el-table-column prop="subject" label="学科"></el-table-column>
        <el-table-column prop="catalog" label="目录"> </el-table-column>
        <el-table-column
          prop="questionType"
          label="题型"
          :formatter="formaQuestionType"
        >
        </el-table-column>
        <el-table-column label="题干" width="280">
          <template v-slot="{ row }">
            <span v-html="row.question"></span>
          </template>
        </el-table-column>
        <el-table-column label="录入时间" width="180">
          <template slot-scope="{ row }">
            {{ row.addDate | formatDate }}
          </template>
        </el-table-column>
        <el-table-column
          prop="difficulty"
          label="难度"
          :formatter="formatDifficulty"
        >
        </el-table-column>
        <el-table-column prop="creator" label="录入人"> </el-table-column>
        <el-table-column label="操作" width="180">
          <template slot-scope="{ row }">
            <el-button
              plain
              icon="el-icon-view"
              size="small"
              type="primary"
              circle
              @click="preview(row)"
            ></el-button>
            <el-button
              plain
              icon="el-icon-edit"
              size="small"
              type="success"
              circle
              @click="$router.push(`/questions/new/${row.id}`)"
            ></el-button>
            <el-button
              plain
              icon="el-icon-check"
              size="small"
              type="warning"
              circle
              @click="addChoice(row)"
            ></el-button>
            <el-button
              plain
              icon="el-icon-delete"
              size="small"
              type="danger"
              circle
              @click="delQuestion(row)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="footer">
        <el-row type="flex" justify="end">
          <el-pagination
            background
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="page.page"
            :page-sizes="[5, 10, 20, 50]"
            :page-size="page.pagesize"
            layout="prev, pager, next, sizes,jumper"
            :total="page.total"
          >
          </el-pagination>
        </el-row>
      </div>
      <!-- 试题预览弹框 -->
      <Preview
        :showPreviewDialog.sync="showPreviewDialog"
        :previewData="previewData"
        :types="previewData.questionType"
        :difficulty="previewData.difficulty"
        :options="checkList"
      ></Preview>
    </el-card>
  </div>
</template>

<script>
import SearchForm from '../components/searchForm.vue'
import { list, detail, remove, choiceAdd } from '@/api/hmmm/questions'

// import { list as getSubjectList } from '@/api/hmmm/subjects'
import { difficulty, questionType } from '@/api/hmmm/constants'
import Preview from '../components/questions-preview.vue'
export default {
  components: {
    SearchForm,
    Preview
  },
  data() {
    return {
      subjectList: [], // 学科列表
      question: [], // 试题列表
      page: {
        page: 1, // 当前页码
        pagesize: 5,
        total: 0
      },
      showPreviewDialog: false, // 控制试题预览弹框
      previewData: {},
      checkList: []
    }
  },
  created() {
    this.getQuestionList(this.page)
  },

  methods: {
    // async getSubjectList() {
    //   const { data: res } = await getSubjectList({ page: 1, pagesize: 20 })
    //   this.subjectList = res.items
    //   console.log(res)
    // },
    // 获取试题列表
    async getQuestionList(data) {
      const { data: res } = await list(data)
      this.question = res.items
      this.page.total = res.counts
    },
    async handleSizeChange(val) {
      // console.log(`每页 ${val} 条`)
      this.page.pagesize = val
      const { data: res } = await list(this.page)
      this.question = res.items
    },
    async handleCurrentChange(val) {
      // console.log(`当前页: ${val}`)
      this.page.page = val
      const { data: res } = await list(this.page)
      this.question = res.items
    },
    // 格式困难度
    formatDifficulty(row, column, cellValue, index) {
      // console.log(difficulty)
      const obj = difficulty.find((item) => item.value === +cellValue)
      return obj ? obj.label : ''
    },
    // 格式题型
    formaQuestionType(row, column, cellValue, index) {
      const obj = questionType.find((item) => item.value === +cellValue)
      // console.log(obj, 222)
      return obj ? obj.label : ''
    },
    // 试题预览
    async preview(row) {
      const { data: res } = await detail(row)
      this.checkList = res.options.map((item) => item.isRight === 1)
      // console.log(this.checkList, 8)
      this.previewData = res
      this.showPreviewDialog = true
    },
    // 删除题库
    delQuestion(row) {
      this.$confirm('此操作将永久删除该试题, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        await remove(row)
        this.getQuestionList()
        this.$message.success('删除题库成功')
      })
    },
    // 加入精选
    async addChoice(row) {
      this.$confirm('确定将此题目加入精选题库?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        await choiceAdd({ ...row, choiceState: 1 })
        // console.log(res, 1)
        this.getQuestionList()
        this.$message.success('加入精选题库成功')
      })
    },
    // 搜索
    async search(data) {
      console.log(data, 789)
      this.getQuestionList(data)
    },
    resetList() {
      this.getQuestionList()
    }
  }
}
</script>

<style scoped lang='scss'>
.container {
  padding: 10px;
  // .el-table {

  //   thead {
  //     color: #909399;
  //     font-weight: 500;
  //     }
  //   }
  //  .has-gutter{
  //     color: #ccc ;
  //     font-weight: 500;
  //  }

  .el-alert {
    margin-bottom: 20px;
  }
}
.footer {
  margin-top: 20px;
}
</style>
